<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>新增导航</title>
		<link rel="stylesheet" href="../../../component/pear/css/pear.css" />
	</head>
	<body>
		<form class="layui-form layui-form-pane" action="" name="frmMain" >
			<div class="mainBox">
				<div class="main-container">
					<div class="layui-form-item ">
						<label class="layui-form-label">导航名称</label>
						<div class="layui-input-block">
							<input type="text" name="nav_name" lay-verify="required" autocomplete="off" placeholder="请输入导航名称"
								class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">导航目标</label>
						<div class="layui-input-block">
							<select name="nav_target" lay-verify="required" lay-filter="res_type">
								<!--导航目标1：嵌入，2：打开新浏览器，3：弹出窗-->
								<option value="">请选择</option>
								<option value="1">嵌入</option>
								<option value="2">打开新浏览器</option>
								<option value="3">弹出窗</option>
							</select>
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">导航图标</label>
						<div class="layui-input-block">
							<input type="text" name="nav_icon" autocomplete="off" placeholder="请输入导航图标"
								   class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">导航链接</label>
						<div class="layui-input-block">
							<input type="text" name="nav_url" autocomplete="off" placeholder="请输入导航链接"
								   class="layui-input">
						</div>
					</div>
				</div>
			</div>
			<div class="bottom">
				<div class="button-container">
					<button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
						lay-filter="save">
						<i class="layui-icon layui-icon-ok"></i>
						提交
					</button>
				</div>
			</div>
		</form>
		<script src="../../../config/apiUrl.js"></script>
		<script src="../../../component/layui/layui.js"></script>
		<script src="../../../component/pear/pear.js"></script>
		<script>
			layui.use(['form', 'jquery','http','popup','common','toast'], function() {
				let form = layui.form;
				let $ = layui.jquery;
				let http = layui.http;
				let popup = layui.popup;
				let common = layui.common;
				let toast = layui.toast;
				let pid = common.getUrlParams("pid");
				form.set({
					verifyError:function (msg){
						toast.error({title:false,message: msg})
					}
				})
				function closeWin(){
					let layer_index = parent.layer.getFrameIndex(window.name);
					if(layer_index>0){
						parent.layer.close(layer_index); //关闭当前页
					}
				}
				$(document).keyup(function (event) {
					if (event.keyCode == 27) {
						closeWin();
					}
				});
				form.on('submit(save)',function(data) {
					layer.confirm('确定要保存导航么', {icon: 3, title: '提示'},async function (index) {
						layer.close(index);
						let loading = layer.load();
						let data_post = data.field;
						data_post.pid = pid;
					    try {
							let ajax_data = await common.ajax({
								url:apiUrlConfig.nav.Add,
								data:JSON.stringify(data_post)
							})
							parent.layui.toast.success({title: false,message: '保存成功'})
							const bc = new BroadcastChannel('navListReload');
							bc.postMessage("navListReload");
							closeWin();
						}catch (e){
							if (e.status == 10001) {
								//字段提示
								let errorMsg = JSON.parse(e.msg);
								for (let key in errorMsg) {
									layer.tips(errorMsg[key], $("[name='" + key + "']").parent(), {
										tips: [3, '#f56c6c'], time: 5000, tipsMore: true
									});
								}
							}
						}
					});
					return false;
				});
			})
		</script>
		<script>
		</script>
	</body>
</html>
